package com.starsmark;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**.
 * 线程操作
 */

public class ThreadOp {

    private static ThreadLocal<Integer> iCounter = new ThreadLocal<Integer>(); // 线程内计数

    private static List<Thread> tList = new ArrayList<Thread>(); // 线程列表
    private static Map<String, Integer> tCountMap = new HashMap<String, Integer>();

    /**.
     * 启动任务
     * @param threadNums 线程数
     * @param reqNums    每个线程请求数
     */
    public void startTaskAllInOnce(int threadNums, int reqNums) {
        Runnable taskTemp = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < reqNums; i++) {

                    // 发请求
                    // String rs = HttpClientUtil.doGet("http://baidu.com");
                    String rs = HttpClientUtil.doGet("http://localhost:8088/css/dashboard.css");
                    System.out.println(rs);
                    if (null != rs) {
                        iCounter.set(i + 1);
                        System.out.println(System.nanoTime() + " [" + Thread.currentThread().getName() + "] iCounter = "
                                + iCounter.get());
                        tCountMap.put(Thread.currentThread().getName(), i + 1);
                    }
                }
            }
        };

        final CountDownLatch startGate = new CountDownLatch(1);
        final CountDownLatch endGate = new CountDownLatch(threadNums);

        for (int i = 0; i < threadNums; i++) {
            Thread t = new Thread() {
                public void run() {
                    try {
                        startGate.await();
                        taskTemp.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        endGate.countDown();
                    }
                }
            };
            tList.add(t);
            tCountMap.put(t.getName(), 0);
            t.start();
        }

        long startTime = System.nanoTime();
        System.out.println("所有线程准备好, 并发运行...");
        System.out.println(startTime + " [" + Thread.currentThread() + "] ");
        startGate.countDown();
        try {
            endGate.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(
                System.nanoTime() + " [" + Thread.currentThread().getName() + "] iCounter = " + iCounter.get());
        long endTime = System.nanoTime();
        System.out.println("所有线程已完成");
        System.out.println(endTime + " [" + Thread.currentThread() + "] ");
        long d = endTime - startTime;
        System.out.println("运行时差: " + d);

        int c = this.calcTotalCount();
        System.out.println("total: " + c);

    }

    /**
     * @return 总请求数
     */
    private int calcTotalCount() {
        int total = 0;
        for (Map.Entry<String, Integer> entry : tCountMap.entrySet()) {
            // System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue());
            total += entry.getValue();
        }
        return total;
    }
}
